package com.csyf.manager.utils.page;

import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 分页的数据转换
 */
public class PageUtils {


    /**
     * 将分页的数据转化成jqgrid 的格式
     *
     * @param list
     * @return
     */
    public static JqGridVo pasrJqGrid(List<?> list) {
        PageInfo<?> sysTradePageInfo = new PageInfo(list);

        JqGridVo jqGridVo = new JqGridVo();
        jqGridVo.setPage(sysTradePageInfo.getPageNum());
        jqGridVo.setRecords(sysTradePageInfo.getTotal());
        jqGridVo.setRows(sysTradePageInfo.getList());
        jqGridVo.setTotal(sysTradePageInfo.getPages());
        return jqGridVo;
    }

    public static void andEqualTo(Example.Criteria criteria, String filed, Object value) {
        if (null != value && StringUtils.isNotBlank(value.toString())) {
            criteria.andEqualTo(filed, value);
        }
    }

    public static void andBetweenDate(Example.Criteria criteria, String filed, Object value) {
        if (null != value && StringUtils.isNotBlank(value.toString())) {
            criteria.andBetween(filed, value + " 00:00:00", value + " 23:59:59");

        }
    }

    public static void andBetweenDateToDate(Example.Criteria criteria, String filed, Object fromDate, Object endDate) {
        if (null != fromDate && StringUtils.isNotBlank(fromDate.toString())
                && null != endDate && StringUtils.isNotBlank(endDate.toString())) {
            //criteria.andBetween(filed, fromDate + " 00:00:00", endDate + " 23:59:59");
            criteria.andBetween(filed, fromDate + " 00:00:00", endDate + " 23:59:59");
        }
    }

    public static void inField(Example.Criteria criteria, String filed, List<String> values) {
        criteria.andIn(filed, values);
    }

    public static void andLikeTo(Example.Criteria criteria, String field, String value) {
        if (null != value && StringUtils.isNotBlank(value.toString())) {
            criteria.andLike(field, value);
        }
    }

}
